<?php
require_once dirname(__FILE__) . '/UsersView.php';
require_once dirname(__FILE__) . '/UsersModel.php';
require_once dirname(__FILE__) . '/../common/Database.php';
require_once dirname(__FILE__) . '/../common/DBConfig.php';
require_once dirname(__FILE__) . '/../login/LoginHandler.php';

class UsersController {
	private $dataBase;
	private $loginHandler;
	
	public function DoController() {
		
		$uv = new UsersView();
		$um = new UsersModel($this->dataBase);
		
		if ($uv->TriedToRegister()) {
			$user = User::create($uv->GetUserName(), $uv->GetFullName(), $uv->GetPassword(), $uv->GetEmail());
			
			if ($uv->RegistrationIsValid()) {
				$success = $um->AddUser($user);
				
				if (!$success) {
					$uv->AddUserExistsError();
				} else {
					$uv->AddUserCreatedMsg($user);
				}				
			}
		}
		
		$html = '';
		
		if (!$this->loginHandler->isLoggedIn()) {
			$html .= $uv->DoRegBox();
		} else {
		
			$users = $um->GetUsers();
			$html .= $uv->OutputUsers($users);
		}
		
		return $html;
	}
	
	public function __construct(Database $dataBase, LoginHandler $loginHandler) {
		$this->dataBase = $dataBase;	
		$this->loginHandler	= $loginHandler;
	}
}
